package GUI;

import domain.DomainController;
import domain.MineField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author "Jeroen"
 */
public class GUIBuilder extends JFrame{
    private DomainController dc;

    public GUIBuilder(DomainController domainController){
        super("Minesweeper");
        
        this.dc = domainController;//MineFieldFactory.getInstance().createMineField();
        
        char[][] field = dc.viewField();
        
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        panel.setLayout(new GridLayout(field[0].length, field.length));
        
        for(int j = 0; j < field.length; j++){
            for(int i = 0; i < field[0].length; i++){
                MineFieldButton btn = new MineFieldButton(i, j);
                btn.setText("");
                btn.addMouseListener(new MouseListenerImpl(minefield,btn));
                panel.add(btn);
            }
            
        }
        
        JPanel outerPanel = new JPanel(new BorderLayout());
        outerPanel.add(panel);
        this.setContentPane(outerPanel);
        
        this.setSize(700,500);
        this.setLocationRelativeTo(null);
        this.setMinimumSize(new Dimension(600,400));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.setVisible(true);
    }
    
}
class MouseListenerImpl implements MouseListener{
    
    MineField m;
    MineFieldButton buttons;
    
    public MouseListenerImpl(MineField minefield, MineFieldButton button){
        m = minefield;
        buttons = button;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = buttons.getX();
        int y = buttons.getY();
        ImageIcon flag = new ImageIcon(getClass().getResource("ico_flag.png"));
        ImageIcon bomb = new ImageIcon(getClass().getResource("ico_mina.png"));
        
        if(SwingUtilities.isLeftMouseButton(e)){
            if(m.getSquare(x,y).isBomb()){
                buttons.setIcon(bomb);
                buttons.repaint();
                buttons.removeMouseListener(this);
                
            }
            if(m.getSquare(x, y).getNearby_bombs() == 0){
                buttons.removeMouseListener(this);
                //blablabla nog te doen
            }
            else{
                buttons.setText(m.getField()[x][y]);
               buttons.repaint();
                buttons.removeMouseListener(this);
            }
                
        }
        if(SwingUtilities.isRightMouseButton(e)){
            buttons.setIcon(flag);
            buttons.repaint();
            buttons.removeMouseListener(this);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    
}
